package com.fingard.dsp.bank.directbank.hbb01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.ITransaction;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7515TodayTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7516HisTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.RetAtsTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.detail.RetAtsTransDetail;
import com.fingard.util.DateHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.util.Calendar;
import java.util.Date;

/**
 * @author Parker on 2019-08-14.
 * @version 1.0
 */
public class HBB01Transaction extends HBB01Base implements ITransaction {

    @Override
    public RetAtsTrans d_7516_HisTrans(Req7516HisTrans p_req7516) throws Exception {
        //历史明细查询返回ATS的对象
        RetAtsTrans retAts = new RetAtsTrans(p_req7516.reqHeader);
        retAts.writeStart();//写返回ATS的统一头部信息
        String tmpRespCode = "";
        String tmpRespInfo = "";
        try {
            Calendar tmpCalendarFrom = Calendar.getInstance();
            tmpCalendarFrom.setTime(p_req7516.getDateFrom());

            Calendar tmpCalendarTo = Calendar.getInstance();
            tmpCalendarTo.setTime(p_req7516.getDateTo());

            int tmpIntDateFrom = 0;
            int tmpIntDateTo = 0;
            //按自然月循环查询
            while(tmpIntDateFrom<=tmpIntDateTo && tmpRespCode.length() == 0){
                Calendar tmpClndrStepTo = Calendar.getInstance();
                tmpClndrStepTo.setTime(tmpCalendarFrom.getTime());
                //判断是否跨月
                if(DateHelper.compareMonth(tmpCalendarFrom, tmpCalendarTo) == -1){
                    //跨月则设置为月末
                    tmpClndrStepTo.set(Calendar.DAY_OF_MONTH, tmpClndrStepTo.getActualMaximum(Calendar.DAY_OF_MONTH));
                }
                else{
                    tmpClndrStepTo.setTime(tmpCalendarTo.getTime());
                }
                //赋值通用参数
                HBB01Header bankHeader = new HBB01Header();
                bankHeader.serviceId = "CBE010201";
                bankHeader.serialNo = getQryTransID();
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                writeCommonHeader(xmlWriter,bankHeader);
                XElement tmpXElement = new XElement("body", "");
                tmpXElement.addChild(new XElement("accountNo", p_req7516.actNum));
                tmpXElement.addChild(new XElement("beginDate", Format.Date8Format.format(tmpCalendarFrom.getTime())));//查询起始日期
                tmpXElement.addChild(new XElement("endDate", Format.Date8Format.format(tmpClndrStepTo.getTime())));//查询结束日期
                xmlWriter.writeXElement(tmpXElement);
                //写入签名数据
                xmlWriter.writeElementString("sign", getSingData(xmlWriter));
                xmlWriter.writeEndDocument();

                String[] tmpRetBkStr = sendToBank(xmlWriter.toXmlString());
                if (tmpRetBkStr[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpRetBkStr[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String returnCode = tmpBkReader.getFirstText("/ebank/head/retCode");
                        String returnMsg = tmpBkReader.getFirstText("/ebank/head/retMessage");
                        if (returnCode != null && returnCode.equals("000000")) {
                            while (tmpBkReader.read("/ebank/body/list/row")) {
                                RetAtsTransDetail tmpRetDetail = new RetAtsTransDetail();
                                XElement tmpRecord = tmpBkReader.readedXElement;

                                tmpRetDetail.actNum = p_req7516.actNum;
                                tmpRetDetail.cdFlag = tmpRecord.getSingleText("intTranAmountType").equals("D") ? "1":"2";//借贷标志
                                tmpRetDetail.tranAmt = Format.DecimalAmtFormat.format(Double.parseDouble(tmpRecord.getSingleText("tranAmt").replace(",", "")));
                                //交易日期
                                String dealDate = tmpRecord.getSingleText("transferDate");
                                if(dealDate.length() >= 8){
                                    tmpRetDetail.tranDate = dealDate.substring(0, 4) + "-" + dealDate.substring(4, 6) + "-" + dealDate.substring(6, 8);
                                }
                                tmpRetDetail.valueDate = tmpRetDetail.tranDate;
                                tmpRetDetail.tranTime = tmpRetDetail.tranDate + " 00:00:00";
                                tmpRetDetail.postTimestamp = tmpRetDetail.tranTime;
                                tmpRetDetail.recAccNum = tmpRecord.getSingleText("intOpAccNo");
                                tmpRetDetail.recAccName = tmpRecord.getSingleText("intOpAccName");
                                tmpRetDetail.accBal = Format.DecimalAmtFormat.format(Double.parseDouble(tmpRecord.getSingleText("balance").replace(",", "")));
                                tmpRetDetail.usage = tmpRecord.getSingleText("summary");
                                tmpRetDetail.comment = tmpRecord.getSingleText("intRemark");
                                tmpRetDetail.abstr = pickupAbstr(tmpRecord.getSingleText("summaryNo"), tmpRetDetail.usage,tmpRetDetail.comment);
                                tmpRetDetail.urid = tmpRecord.getSingleText("vouchersNo");
                                retAts.writeRetDetail(tmpRetDetail);
                            }
                        } else {
                            tmpRespCode = "-1";
                            tmpRespInfo = returnMsg;
                            break;
                        }
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = tmpSbMsg.toString();
                        break;
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpRetBkStr[1];
                    break;
                }

                tmpCalendarFrom = tmpClndrStepTo;
                tmpCalendarFrom.add(Calendar.DAY_OF_MONTH, 1);
                tmpIntDateFrom = Integer.parseInt(Format.Date8Format.format(tmpCalendarFrom.getTime()));
                tmpIntDateTo = Integer.parseInt(Format.Date8Format.format(tmpCalendarTo.getTime()));
            }
        }catch (Exception ex) {
            ex.printStackTrace();
            tmpRespCode = "-1";
            tmpRespInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        if ("-1".equals(tmpRespCode)) {
            retAts = new RetAtsTrans(p_req7516.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts = new RetAtsTrans(p_req7516.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode("0");
            retAts.writeRespInfo("交易为空!");
        }

        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsTrans d_7515_TodayTrans(Req7515TodayTrans p_req7515) throws Exception {
        //通用历史余额查询
        Req7516HisTrans tmpReq7516 = new Req7516HisTrans(p_req7515.reqHeader);
        tmpReq7516.actArea = p_req7515.actArea;
        tmpReq7516.actCur = p_req7515.actCur;
        tmpReq7516.actNum = p_req7515.actNum;
        String tmpStrToday = Format.Date8Format.format(new Date());
        tmpReq7516.dateFrom = tmpStrToday;
        tmpReq7516.dateTo = tmpStrToday;
        return 	d_7516_HisTrans(tmpReq7516);
    }
}
